package com.more.offer.algorithm.leetcode.lcode02;

/**
 * @Author: yinpeng
 * @Date: 2019/9/24 15:42
 * @Description: TODO
 **/
public class Solution {
	public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode res = new ListNode(0);
		ListNode a1 = l1, a2 = l2, ret = res;
		int temp = 0;
		while (a1 != null || a2 != null) {
			int x = a1 != null ? a1.val : 0;
			int y = a2 != null ? a2.val : 0;
			int sum = x+y+temp;
			temp = sum/10; //是否进位
			ret.next = new ListNode(sum%10);
			ret = ret.next;
			if(a1!=null) a1 = a1.next;
			if(a2!=null) a2 = a2.next;
		}
		if(temp>0){
			ret.next = new ListNode(temp);
		}
		return res.next;
	}

	public static void main(String[] args) {
		ListNode l1 = new ListNode(2);
		l1.next = new ListNode(4);
		l1.next.next = new ListNode(3);

		ListNode l2 = new ListNode(5);
		l2.next = new ListNode(6);
		l2.next.next = new ListNode(4);

		ListNode listNode = addTwoNumbers(l1, l2);
		System.out.println(listNode.val+" -> "+listNode.next.val+" -> "+listNode.next.next.val);
	}
}
